﻿//
// *************************************************************************************
// WP-Framework
// 
// Developed by
// Lukas Kretschmar (lkretschmar.dev@bluewin.ch)
//
// Switzerland, Copyright (c) 2013 lkretschmar
// This content is released under the MIT License (http://opensource.org/licenses/MIT).
//     
// *************************************************************************************
//
//


using System.Collections.Generic;
using KretschIT.WP_Fx.EasyMoq.Generator.Items;
using KretschIT.WP_Fx.Core.Validation;
using System.Linq;
using KretschIT.WP_Fx.EasyMoq.Generator.Usings;
using System;

namespace KretschIT.WP_Fx.EasyMoq.Generator
{
    public class EasyMoqTemplateHelper : IEasyMoqTemplateHelper
    {
        private IEasyMoqConfigReader configReader;

        public EasyMoqTemplateHelper(IEasyMoqConfigReader configReader)
        {
            It.IsNotNull(() => configReader);

            this.configReader = configReader;
        }

        public string RegistrationAttributeName
        {
            get { return typeof(EasyMoqRegistrationAttribute).Name.Replace("Attribute", string.Empty); }
        }

        public string InitClassName
        {
            get { return "EasyMoqInit"; }
        }

        public string TargetNamespace
        {
            get { return this.configReader.TargetNamespace; }
        }

        private IEnumerable<IMockableInfo> mockableInfo;

        public IEnumerable<IMockableInfo> MockableInfo
        {
            get
            {
                if (this.mockableInfo == null)
                {
                    this.mockableInfo = this.configReader.GetMockableInfo();
                }
                return this.mockableInfo;
            }
        }

        public IEnumerable<string> FactoryUsingNamespaces
        {
            get 
            {
                return FactoryUsings.Usings;
            }
        }

        private IUsingsBuilder FactoryUsings
        {
            get
            {
                var usings = new UsingsBuilder();
                usings.Add(this.MockableInfo.Select(m => m.InterfaceType));
                return usings;
            }
        }


        public IEnumerable<string> MockablesUsingNamespaces
        {
            get
            {
                var usings = new UsingsBuilder();
                usings.Add(this.MockableInfo.SelectMany(m => m.UsingNamespaces).Distinct());
                usings.Remove(this.FactoryUsings);
                return usings.Usings;
            }
        }

        public INamingFactory NameFactory
        {
            get { return this.configReader.NameFactory; }
        }
    }
}
